* Table A10: Intergenerational Mobility in the 1948-1953 and 1961-1964 Cohorts, Multiply Imputing Missing Income Reports


eststo clear
set seed 9160
set sortseed 9385

clear
use "${data_dir}DavisMazumderData.dta"
keep if age_firstSurvey<=18
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 

gen id = .
 replace id = id_son if surv79==0 & women==0
 replace id = id_daughter if surv79==0 & women==1
 replace id = CASEID if surv79==1
 
isid id surv79 women


keep id kid1 kid2 kid3 parent1 parent2 parent3 mom_age_at_birth_b dad_age_at_birth_b ms_kid1 ms_kid2 ms_kid3 ms_parent1 ms_parent2 ms_parent3 weight hhid surv79 women black married hgc

gen hgc_ms = missing(hgc)
replace hgc = 0 if missing(hgc)

gen married_ms = missing(married)
replace married = 0 if married_ms==1

gen hgc2 = hgc*hgc

gen mom_age_at_birth_b2 = mom_age_at_birth_b*mom_age_at_birth_b
gen dad_age_at_birth_b2 = dad_age_at_birth_b*dad_age_at_birth_b


mi set flong

/* Register Which Variables Will and Will Not Be Imputed */
mi register imputed kid1 kid2 kid3 parent1 parent2 parent3
mi register regular mom_age_at_birth_b dad_age_at_birth_b ///
	ms_kid1 ms_kid2 ms_kid3 ms_parent1 ms_parent2 ms_parent3 ///
	mom_age_at_birth_b2 dad_age_at_birth_b2 black married hgc hgc2 hgc_ms married_ms



/* Impute  */
 mi impute chained (pmm, knn(10)) kid1 kid2 kid3 parent1 parent2 parent3 ///
	=  mom_age_at_birth_b  mom_age_at_birth_b2 dad_age_at_birth_b  ///
		dad_age_at_birth_b2 black married hgc hgc2 ///
		ms_kid1 ms_kid2 ms_kid3 ms_parent1 ms_parent2 ms_parent3 ///
	, by(surv79 women) add(20) rseed(1272886) force
	
	* Imputed log income 
	 mi passive: egen faminc0 = rowmean(parent1 parent2 parent3)
	 mi passive: egen faminc1 = rowmean(kid1 kid2 kid3)
	 gen lfaminc0 = log(faminc0)
	 gen lfaminc1 = log(faminc1)
	 
	 * Imputed Ranks
	 gen n=1
	 gen N = .
		tab n  if surv79==0 & women==0 & _mi_m==1 [w=weight]
		replace N = r(N) if surv79==0 & women==0
		tab n  if surv79==0 & women==1 & _mi_m==1  [w=weight]
		replace N = r(N) if surv79==0 & women==1
		tab n  if surv79==1 & women==0 & _mi_m==1  [w=weight]
		replace N = r(N) if surv79==1 & women==0
		tab n  if surv79==1 & women==1  & _mi_m==1 [w=weight]
		replace N = r(N) if surv79==1 & women==1
 
	set sortseed 6688571
	sort _mi_m women surv79 faminc0
	by _mi_m women surv79: gen __rank0 = sum(weight) 
	gen _rank0 = __rank0/N 
	bys _mi_m women surv79 faminc0: egen rank0 = mean(_rank0) 
 
	set sortseed 6688571
	sort _mi_m women surv79 faminc1
	by _mi_m women surv79: gen __rank1 = sum(weight) 
	gen _rank1 = __rank1/N 
	bys _mi_m women surv79 faminc1: egen rank1 = mean(_rank1) 


drop N __rank0 _rank0 __rank1 _rank1

	
	 mi passive: gen perc66  = rank0*(surv79==0)
	 mi passive: gen perc79  = rank0*(surv79==1)	
	 mi passive: gen loginc66  = lfaminc0*(surv79==0)
	 mi passive: gen loginc79  = lfaminc0*(surv79==1)	 
	 
	gen lfaminc0_ms   = missing(lfaminc0)
	bys surv79 women id: egen any_ms0 = max(lfaminc0_ms)
	gen lfaminc1_ms   = missing(lfaminc1)
	bys surv79 women id: egen any_ms1 = max(lfaminc1_ms) 


eststo clear	
	
	* --------- Rank-Rank --------- *

* Pooled
 mi estimate, cmdok post esample(s): reg rank1 perc66 perc79 surv79 women [w=weight] , cluster(hhid)
eststo rank_p
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_p
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_p
drop s

 mi estimate (_b[perc79]-_b[perc66]), cmdok post: reg rank1 perc66 perc79 surv79 women [w=weight] , cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: rank_p
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): rank_p
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): rank_p
global rank_main_est = r(estimate)
global rank_main_se = r(se)


* Women
 mi estimate, cmdok post esample(s): reg rank1 perc66 perc79 surv79 if women==1 [w=weight] , cluster(hhid)
eststo rank_w
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_w
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_w
drop s

mi estimate (_b[perc79]-_b[perc66]), cmdok post: reg rank1 perc66 perc79 surv79 women [w=weight] if women==1 , cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: rank_w
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): rank_w
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): rank_w


* Men
 mi estimate, cmdok post esample(s): reg rank1 perc66 perc79 surv79 if women==0 [w=weight] , cluster(hhid)
eststo rank_m

count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_m
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_m
drop s

mi estimate (_b[perc79]-_b[perc66]), cmdok post: reg rank1 perc66 perc79 surv79 women [w=weight] if women==0 , cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: rank_m
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): rank_m
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): rank_m

* Drop if imputation is ever negative
drop if any_ms0==1 | any_ms1==1



* --------- IGE --------- *

* Pooled
 mi estimate, cmdok post esample(s): reg lfaminc1 loginc66 loginc79 surv79 women [w=weight], cluster(hhid)
eststo IGE_p

count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_p
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_p
drop s

mi estimate (_b[loginc79]-_b[loginc66]), cmdok post: reg lfaminc1 loginc66 loginc79 surv79 women [w=weight], cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: IGE_p
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): IGE_p
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): IGE_p
global ige_main_est = r(estimate)
global ige_main_se = r(se)

* Women
 mi estimate, cmdok post esample(s): reg lfaminc1 loginc66 loginc79 surv79 if women==1 [w=weight], cluster(hhid) 
eststo IGE_w

count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_w
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_w
drop s

mi estimate (_b[loginc79]-_b[loginc66]), cmdok post: reg lfaminc1 loginc66 loginc79 surv79 women [w=weight] if women==1, cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: IGE_w
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): IGE_w
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): IGE_w

* Men
 mi estimate, cmdok post esample(s): reg lfaminc1 loginc66 loginc79 surv79 if women==0 [w=weight], cluster(hhid)
eststo IGE_m

count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_m
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_m
drop s

mi estimate (_b[loginc79]-_b[loginc66]), cmdok post: reg lfaminc1 loginc66 loginc79 surv79 women [w=weight] if women==0, cluster(hhid)
estadd scalar diff = e(b)[1,2]-e(b)[1,1]: IGE_m
estadd scalar se = sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2]): IGE_m
estadd scalar p = 2*normal(-(e(b)[1,2]-e(b)[1,1])/sqrt(e(V)[1,1]+e(V)[2,2]-2*e(V)[1,2])): IGE_m

#delimit ;
estout rank_p IGE_p rank_w IGE_w rank_m IGE_m
	using  "${results_dir}tableA10.txt", replace
	keep(perc66 perc79) 
	rename(loginc66  perc66 loginc79 perc79)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(diff se p N66 N79) stardrop(*) ;	
#delimit cr	



exit













